package college;

import clientserver.Client;
import clientserver.Packet;
import clientserver.QueryResult;
import college.general;
/**
 * Class handles the system messages transfered throughout the system.
 * @author Group11
 *@see Client
 */
public class SysMessages 
{

private Client c;	
	
public SysMessages(Client cl)
{
c = cl;	
}

/**
 * Set a message that will be sent to a user.<p>
 * Updates the database.
 * @param S - Message to be sent.
 * @param login - User that the message will be sent to.
 * @see User
 * @see Packet
 */
public void setmsg(String S,String login)
{
	String q;
	q = "Update sysmessages s Set s.message = '"+S+"' Where tologin = '"+login+"'" ;
	Packet p=new Packet(q);
	 p.setUpdatePacket();
	 c.SendPacketNowait(p);
	
}

	


/**
 * Function checks whether a certain login has a message waiting and sends an alert accordingly.
 * @param login - Current user login.
 * @see User
 */
public void getmsg(String login)
{
	
	QueryResult t;
	String res="";
	String str = "SELECT * From sysmessages s  Where s.tologin ='"+ login+"'";
	System.out.println(str);
	 Packet p=new Packet(str);

	  t=(QueryResult) c.SendPacketAndWait(p);
	       for( int i=0;i<t.rowsize();i++)
	       {
	    	   String mm=t.getString("message");
	  		  res=res+mm;
	    	      
	       }
	System.out.print(res);
	if(!res.isEmpty())
	general.alert(res);
}

	
	
}
